package com.gla.market.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.entity.ShopCart;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.text.DecimalFormat;
import java.util.List;

public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.ViewHolder>{

    private List<ShopCart> cartList;


    public OrderItemAdapter(Context context,List<ShopCart> list) {
        this.cartList = list;
        //实例化ImageLoader
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
        ImageLoader.getInstance().init(configuration);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_commodity, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
        ShopCart shopCart = cartList.get(i);
        ImageLoader.getInstance().displayImage(shopCart.getCommodity().getPhoto(), holder.ivShowPic);
        holder.tvCommodityType.setText("类别："+shopCart.getCommodity().getType().getName());
        holder.tvCommodityName.setText(shopCart.getCommodity().getName());
        holder.tvCommodityNum.setText(" X" + shopCart.getNumber() + "");
        DecimalFormat df = new DecimalFormat("0.00");
        String price = df.format(shopCart.getCommodity().getPrice());
        holder.tvCommodityPrice.setText("￥"+price);
        holder.tvCommodityIntroduction.setText("简介："+shopCart.getCommodity().getIntroduction());
    }

    @Override
    public int getItemCount() {
        return cartList.size();
    }

    //初始化控件
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivShowPic;
        TextView tvCommodityName, tvCommodityType, tvCommodityPrice, tvCommodityNum,tvCommodityIntroduction;

        public ViewHolder(View itemView) {
            super(itemView);
            ivShowPic = itemView.findViewById(R.id.iv_show_pic);
            tvCommodityName = itemView.findViewById(R.id.tv_commodity_name);
            tvCommodityType = itemView.findViewById(R.id.tv_commodity_type);
            tvCommodityIntroduction = itemView.findViewById(R.id.tv_commodity_introduction);
            tvCommodityPrice = itemView.findViewById(R.id.tv_commodity_price);
            tvCommodityNum = itemView.findViewById(R.id.tv_commodity_num);
        }
    }
}

